﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using NetDimension.Weibo;
using System.Timers;
using System.IO;
using System.Net;
using System.Xml;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Collections;
using System.Drawing;
using System.Diagnostics;


namespace WeiboTimer
{
    public partial class Form1 : Form
    {
        static string LoginName = string.Empty;
        static string PassWord = string.Empty;
        static string AppKey = string.Empty;
        static string AppSecret = string.Empty;
        static string CallbackUrl = string.Empty;

        static string AccessToken = string.Empty;

        static OAuth oauth = null;
        static Client Sina = null;

        static Logger iLogger = new Logger();
        static XML ixml = new XML();
        static Picture iPicture = new Picture();
        static Sqlite iSqlite = new Sqlite();
        static MidToId iMidToId = new MidToId();
        static WaterMark iWaterMark = new WaterMark();

        static string dataBaseFile = "Databases/Weibo.sqlite";
        static string waterMarkFile = "watermark.jpg";
        static System.Timers.Timer t = new System.Timers.Timer();
        static string myUid = "";
        static string myNickName = "";
        static Random ro = new Random();

        static int atCount = 0;
        static List<string> listx = new List<string>();

        static bool AutoComment = false;    //自动评论
        static bool AutoWaterMark = false;  //自动加水印

        NotifyIcon notifyicon = new NotifyIcon();   //创建托盘图标对象 
        //Icon ico = new Icon("WeiboTimer.ico");
        ContextMenu notifyContextMenu = new ContextMenu();//创建托盘菜单对象 

        public Form1()
        {
            InitializeComponent();
            iLogger.form1 = this;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.notifyIcon1.Text = "WeiboTimer";
            //测试代码

            //读取配置
            LoginName = ixml.Read("LoginName");
            PassWord = ixml.Read("PassWord");
            AccessToken = ixml.Read("AccessToken");
            AppKey = ixml.Read("AppKey");
            AppSecret = ixml.Read("AppSecret");
            CallbackUrl = ixml.Read("CallbackUrl");
            if (LoginName == "" || PassWord == "" || AppKey == "" || AppSecret == "" || CallbackUrl == "")
            {
                iLogger.logger("配置信息不完整，程序退出！");
                return;
            }

            //验证数据库文件
            if (!iSqlite.CheckDbFile(dataBaseFile))
            {
                iLogger.logger("数据库文件不存在，程序退出！");
                return;
            }

            //验证授权
            if (!GetAccessToken())
            {
                iLogger.logger("无法获得授权，程序退出！");
                return;
            }
            else
            {
                Sina = new Client(oauth);
                Thread.Sleep(1000);
            }

            //获取自身信息
            try
            {
                var entity_RateLimitStatus = Sina.API.Entity.Account.RateLimitStatus();
                iLogger.logger("接口剩余调用次数：" + entity_RateLimitStatus.RemainingIPHits.ToString());
                myUid = Sina.API.Entity.Account.GetUID();
                var entity_userInfo = Sina.API.Entity.Users.Show(myUid);
                myNickName = entity_userInfo.ScreenName;
                iLogger.logger("uid：" + myUid + ", 昵称：" + myNickName);
                button1.Enabled = false;

#if DEBUG
                if (1 == 1)
                {
                    Sina.API.Entity.Statuses.UploadUrlText("遇到一个人，叫温暖。", "http://ww3.sinaimg.cn/bmiddle/6628711bjw1e0mcu87anqj.jpg");
                    //SyncWeibo("1713926427");
                    //CommnetWeibo("1197161814", "test");

                    //string topUid = "1266321801,1704116960,1195242865,1192329374,1282005885";
                    //string[] splittopUid = topUid.Split(',');

                    //for (int i = 0; i < splittopUid.Length; i++)
                    //{
                    //    GetAtSomebodyFromUid(splittopUid[i]);
                    //}
                    //iLogger.logger("完成临时任务，退出！");

                    return;
                }
#endif

            }
            catch (Exception ex)
            {
                iLogger.logger("调用API错误：" + ex.Message + "，程序退出！");
                return;
            }

            #region timer
            try
            {
                t.Interval = 1000 * 60;      //设置间隔时间为1分钟
                //t.Interval = 1000;          //调试时间：1秒钟
                t.Elapsed += new ElapsedEventHandler(TimerElapsedEvent);   //到达时间的时候执行事件； 
                t.AutoReset = true;         //设置是执行一次（false）还是一直执行(true)； 
                t.Start();                  //启动Timer对象； 
            }
            catch (Exception ex)
            {
                iLogger.logger("定时异常：" + ex.Message);
                t.Stop();
            }
            #endregion

            iLogger.logger("定时任务开始......");
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
         
        }

        #region Debug
        void debugFunction()
        {
            Thread thLoad = new Thread(new ThreadStart(delegate()
            {
                this.UIInvoke(() =>
                {
                    string topUid = "1266321801,1704116960,1195242865,1192329374,1282005885,1656809190,1793285524,1195230310,1192515960,1087770692,1197161814,1742727537,1730336902,1682352065,1763582395,1784537661,1223762662,1739928273,1362607654,1195354434,1188552450,1854283601,1752467960,1722594714,1182391231,1744825334,1644492510,1241148864,1937439635,1234552257,1608574203,1774978073,1733838873,1223178222,1730077315,1228486722,1642351362,1681213010,1198920804,1196235387,1816011541,1684388950,1249193625,1212812142,1686326292,1380274560,1722656062,1726644942,1866882821,1644250693,1245758104,1752502537,1230663070,1581391477,1235733075,1182389073,1632798537,1265020392,1649005320,1517934944,1260194414,1191044977,1257818405,1692544657,1563815015,1345467925,1291843462,2044679991,1821257283,1750428112,1826513532,1322920531,1731972042,1670071920,1777924625,1747514562,2145291155,1707679453,1768249682,1220924217,1740006601,1692801804,1878384807,1892464897,1761047370,1778742953,1253491601,1275017594,1727548672,1717675430,1650569064,1193491727,1712539910,1162178432,1220926753,1194869670,1751035982,1270344441,1470110647,1303977362,1198251274,1689219395,1289997621,1413971423,1222713954,1768305123,1271542887,1662055430,1266286555,1270468784,1187986757,1285846970,1196126832,1746274673,1719232542,1785075474,1234692083,1610536214,1515817390,1790596553,1261700994,1831216671,1729853157,1751309223,1384952882,1197931472,1734255860,1230928585,1194026233,1986010511,1764033053,1255849511,1191220232,1189615035,1265057942,1373421990,1229385395,1742741072,1676082433,1277127435,1195366567,2134671703,1299532580,1895570203,1773283005,1361074815,1247685455,1657776532,2010223434,1380265057,1495033135,1938284521,1721825977,1647678107,1762331644,1787715710,1644419370,2130434435,1743938470,1684502353,1049198655,1003716184,1644461042,1156966391,1664207987,1195210033,1315469422,1195031270,1345566427,1344360230,1689369091,1878546883,1814259927,1213058731,1195818302,1900093290,1216388157,1755370981,1748693152,1106587564,1259193624,1504965390,1782705647,1671342103,1296234914,1730726637,1813482925,1312412824,1831348402," +
                       "1669763744,1036663592,1213395722,1182419921,1646239802,1708942053,1803658131,1280761142,1193111400,1756505825,1686990012,1271117655,1749127163,1905077071,1797007051,1431680140,1191312074,1577815384,1717175282,1197069394,1711523125,1193258875,1254433995,1288640027,1859363691,1237869662,1726602910,1304194202,1195403385,1211628752,1199430302,1741492385,1254745922,1705338574,1711684227,1327191953,1648764935,1213452333,1630856882,1859353001,1284032347,1193256814,1275784277,1634785555,1864388983,1307651590,1170467832,1104503584,1194868525,2289674265,1839673641,1700162184,1219952281,1686532492,1042026447,1262456801,1221530313,1791346245,1402602034,1738261305,1735902985,1824553760,1774252962,1241679004,1687429295,1642630543,1611215014,1645171780,1663363857,1872011975,1712826100,1742912084,1859674134,1822796164,2247657845,1219701947,1249424622,1989660417,1834047087,1271394395,1601563722,1707691442,1494848464,1642326133,1262486753,1263762522,1689023367,1252036043,1706987705,1711918131,1226321165,1812591014,1724196104,1650450024,1198404223,1511484000,1676707167,1197369013,1246788271,1655009693,1656316457,1772439462,1798145045,1707178184,1594643405,1569160390,1805464273,1604179804,1340406064,1877716733,1812557667,1834628251,1829744931,1197151844,1684012243,1307907720,1173544292,1689592544,1929655555,1195224593,1687003614,1785819763,1196624615,1806080943,1662717013,1374834850,1288803057,1928240144,1682804733,1702129627,1785884091,1885355607,1291477752,1922542315,1803814487,1712352650,1496850204,1182415487,1712975451,1104150515,1290193560,1662450871,1792617865,1719481457,1662558237,1223898097,1640571365,1736275134,1665074831,1726616225,1738933752,1866179020,1193476843,1894947983,1193258161,1221987632,1717385570,1227636382,1719252400,1721860370,1396454421,1742907811,1890443153,1663512401,1309388607,1248351970,1189729754,1210604004,1590756795,1738770705,1296241304,1197890497,1657464934,1900022001,1197199510,2550756250,1040482460,1687297134,1442054902,2291250415," +
                       "1712354525,1227086635,1717858734,1649166140,1288089225,1640376730,1838993640,1741069204,1191258655,1704091601,1657022395,2339140841,1632067682,1378915695,1269402871,1682346610,1909622397,1806062307,1749964961,1192503217,1892744317,1892499233,1423424121,1801920002,1211710022,1699521851,1197755162,1195813085,1866402485,1265241322,1254123322,1803519527,1499102183," +
                       "1360749324,1195300800,2345544584,1221895385,1497847721,1850145517,1231685824,1253242934,1214435497,1913624330,1747008532,1851666941,1878923963,1651459561,1809680753,1794824567,1736105723,1525396581,1661938815,1655493061,1689040213,1501634660,1256947091,1679137870,1238712252,1252340685,1243440512,1759084801,1772782534,1892496057,1598524121,2081230711,1767062680,1356289201,1234568682,1756881315,1680902912,1786700611,1254139301,1217743083,2264358493,1250877663,1557721227,1223081970,1357579865,1780535520,1350947532,1885257803,1767981164,1665981543,1927706047,1900093822,1196474352,1197071940,1884616407,1620412303,1192329893,1318745373,1729985405,1863057363,1714878522,1240375710,2157238653,1636151307,1615670221,1257355460,1680448231,1898472192,1691954411,1550877863,1779875541,1234682582,1402271902,1004524612,2203023924,1195389671,1705586121,1661558660,1190953227,1660386667,1241996937,1251000504,1350537007,1213327341,1871721375,1662429125,1865844054,2179825195,2159863893,1302670237,1717748707,1723291702,1270873377,1233536692,2258248407,1361024910,1964959441,1911361075,1740573077,1498502972,1659631214,1726826455,1223179064,1916383397,1497293604,1575317851,1774859483,1145662840,1771539483,1221156585,1692056982,1227655400,1766938004,2364309274,1558226504,1787530255,1930201220,1717759773,1188297871,1908248274,1198248427,1969957151,1249622393,1743040097,1806762625,1806806055,1198213514,1490372962,1706948687,1263730567,1263585717,1649543975,1229884532,1303154437,1197964571,1907980965,1236392942,1527202452,1676794614,1679602805,1215787800,1498441097,1668581575,1574684061,1841518257,1671248621,1195301385,1744501565,1733275311,1707743230,1396715380,1578061435,1198480545,1785802725,1870557227,1717629720,1192148495,1874320010,2088912225,1240901011,1577783393,1697717391,1763563324,1657882770,1647497355,1213239282,1719712824,1246279100,1773286334,1189852602,1229384287,1220937192,1796623944," +
                       "1197930844,1497042545,1644051882,1617902267,1051159620,1890985971,1506088515,1234739511,1802726235,1225205711,1854225961,1672587573,1716658720,1645226103,1191808911,1994614105,1198392970,1224672814,1404927945,1789362170,1928273515,1214555081,1704518341,2113045567,1592911662,1316949123,1746664450,1670697373,1867519663,1874356165,1225907163,1962644273,1885823495,1900195764,1342829361,1676455960,1695247672,1661707834,1233614454,2529348553,1853632123,1191007840,1995801167,1408850565,1719169913,1240959311,1213328234,1878506311,1455643221,1767635255,1665736437,1711116915,2113076563,1195017937,1132891981,1279884602,1218869573,1191979833,1644684164,1827685703,1252699553,1659814531,1775644157,1096102210,1851875254,2283720217,1747506517,1378010100,2267500312,2344099161,1496867634,1781718962,1697139145,1781789543,1656927847,1668576570,1570722975,1687056522,1776845763,1347118243,2031473245,1745395767,1322373644,1748336974,1239246050,1228977142,1799833402,1811214312,1850157291,1003763994,1253128043,2302663124,1249230817,1400722343,1082139937,1398886745,1752037362,1751358725,1822225630,1654716241,1627884895,1228131382,1895264740,1756644751,1390408777,1889614173,1244856617,1662766362,1228322981,2076169717,1653714227,1430367804,2287349412,1051170950,1393786362,1885499793,1650701263,1199429884,1806722615,1769787285,1303599847,1227368500,1195379710,1216416785,1907244215,1278193203,1496864063,1644215181,1829886203,1196975644,1198253935,2171551267,1255687540,1897435043,1223537940,1851497955,1770438374,1764938975,2192606857,1689244230,1855593723,1852888223,1231687250,2087497631,1642586513,1596103295,1212612565,1989412233,1640365687,1823508995,1600096707,1459022000,1713100305,1874930452,1727253805,2029723712,1405827991,1895918803,1832598427,1250041105,1197929090,1678086323,1859651060,1838364837,1648083434,1664321624,1847428005,1730330447,1704392330,1837017107,1282282193,1632538541,1672330664,1257866011,1639036967,1644034223,1878506965,1627572147,1886706617,1700775127,1216541943,1793919247,1846722212,1620029235,1057581555,1210348320,1814284757," +
                       "1761684925,1644857285,1765296764,1811891453,1688527963,1919014323,1602756051,2041511423,1112492684,1419092951,1928962635,1276314124,1844705657,1865893201,1191042674,1781593357,1223208794,1801055875,1626167435,1960869301,1268293683,1916318985,1195019732,2158779561,1674176955,1627601305,1821949257,1258824907,1711759320,1254764477,1568061967,1922233745,1715674341,1197951667,1237973207,1268411390,1943342045,1036043550,1653974425,1713961440,1339440642,1640631034,1647688175," +
                       "1005592945,2125629715,1729736051,1657288682,1497082935,2302554251,1885448847,1763969373,1899009277,1223450301,1231067520,1192336151,1770229981,1769400853,1826628633,1649393195,1741657627,1246447601,1240780154,1866583215,1298140275,1451306295,1321880115,1793569311,1907832993,1369781132,1294053670,1195231190,2018925253,1225419417,1198951745,1835408957,1249570203,1189711610,1219459467,1988314691,1263498570,1654650662,1714925101,1652679077,1230707503,1841835853,1679001597,1760984890,1212642787,1568078100,1221073213,1802406901,1759973613,1896697435,1778380375,1271132954,1195230942,1232164963,1921089310,1217261134,1663932242,1481028245,1757052517,1246151574,1683652260,1802742227,1943306413,1898386062,1946109133,1878247607,1483820045,1563092034,1193945713,2097482553,1281965207,1392647332,1634431184,1469986817,1594779263,1081915300,1732460543,1723141197,2002599933,1920031245,1703859825,1219847813,1877967664,1660366351,1497882593,1815820173,1265149745,1220862142,1212641822,1168031893,1828951263,1543596282,1880143303,1699316691,1236930591,1311594930,1007343817,1734317652,1736042541,1762525932,1771544643,1682194877,1313697151,1732235143,1371858833,1180323217,1891453505,2030371897,2506966302,2018220685,1863010390,1252032243,1802677091,1218339187,1066934892,1401154487,1284664183,1721396755,1232114604,2308438637,1861677792,2084009251,1658715641,1733698064,1932081901,1218639357,1266153905,1708309817,1445681564,1211598953,1802982701,1913450052,1340243584,1775564374,1877375603,1494832634,1172294045,1216098513,1497185040,1279776120,1024763102,1862468832,2078567085,1884277373,1701619985,1714193955,1197191492,1646270104,1718813623,1060603161,1736184365,1220824437,1840783491,1222029033,1197365250,1024431497,1292500037,1708242847,1738171957,1903013424,1275535607,1134880297,1748755003,1400617000,1348026261,1496919457,1661987395,1273380037,1063988882,1196267795,1795196332,1760125267,1644981421,1420174783,1496868001,2201594645,1822836700,1863847262,1198367585,1834479492,1732053592,1709498127,1277934435,2280849685,1501877483,1253263117,1757773171,1873360037,1688041835,1670970787," +
                       "2344083321,1597044620,1745573472,1278849821,1496825941,1500662492,1251302465,1801465950,1727703612,1708161521,1568904522,2206048465,1714176194,1732509302,1564844420,1686538974,1566455250,1224529374,1647933321,1731769015,1851615057,2290173077,1433765237,1650305567,1235919683,1999393467,2295389250,2337206513,2396981212,1746901070,1874556373,1711955934,1202435843,1696386523,1498522714,1255260021,1839963312,1252770944,1195228667,1660235667,1708427087,1298230512,1725910795,1107198435,1737488713,1621444281,1819746910,1725225367,1653556670,2112642651,1280546287,1195381367,1622643194,1986357373,1000440717,1213529407,1735431001,1793335483,1737440957,1801631004,1603238494,1268453601,1237627042,2039543177,2078059940,1649328410,1347949163,1648065683,2100290907,1094627834,1692091970,2373686235,2039141565,1642629063,1700420845,1246850033,2265735423,1992473700,1750082023,1396928042,1193729412,1239315002,1400340051,1510050685,1210297085,1644479674,1335936365,1917860675,2080927463,1764946060,1752778612,1739814677,1705362162,1582319552,1853073072,1863047190,1218698200,1244589914,1248284645,1054007001,1861112922,1660399881,2005186035,1935107377,1558231133,1606692571,1735833683,1729078444,1496883920,1047183715,1025103297,1309028787,1405094090,1664426741,1647258043,1845675654,1712295232,1759094663,1830675705,1497044307,1221485670,1648844612,2261168751,1879502500,1894349567,1997726860,1655843694,1839654087,1582922582,1497390470,1593780282,1622549521,1594745502,1724962892,1660352010,1640552915,1997641692,1052440767,1194894893,1722803755,1171418901,1359703040,1868911504,1864187232,1749658917,1703704777,1270623893,1522622622,1764105342,2290298244,2042463657,1735135037,1268356760,1748963770,1725202627,2014034660,1229327625,1595453585,2646163860,1959441800,1247927184,2192625807,1749275353,2044620715,1786977895,1750779917,1764361741,1305435283,1800816823,1864605465,1980337107,1263835934,1752051530,1756505647,1628867530,1724423673,1780853205,1214077224,1596409212,1510126321,1195201334,2041418617,1196129047,1779228023,1677674353,1815624851,1730283472,1213536224,1218149847,1245849705," +
                       "1845464242,1775466497,1477045392,2176674215,1767765052,1290700430,1659809154,2147283903,2043737667,1198255227,1152531173,1070952141,1308197681,1670079195,1243115577,1899681264,1508173421,2307505790,1254127560,1867422881,1659064660,1917274543,1738089272," +
                       "1280794714,2294731913,1870370853,2377430633,1888655105,1904716254,1841424431,1310876824,1312616861,1739643621,1847958501,1224685661,1262449494,1662078983,1808389121,1660399951,1708255390,1759023505,1666366480,1574525494,1244876825,1623412013,1851563345,1256741783,2297199692,1220740480,1256857734,1559383255,2065625733,1654192490,1887751271,1337925752,1670419227,1680313495,1748475757,1837025515,1215087445,1743197323,1227627711,1910238847,1316018310,1234694902,1667502093,1656565920,1701677474,1788531474,1260165344,2093662625,1286562485,1840172567,1193257803,1630069961,1278178992,1729714591,1893662124,1726396365,1641337330,1768340073,1846360445,1076196517,1358573677,1803553017,1732418574,1750119550,1707759510,2103206685,2236753302,1904008103,1272621567,1800292762,1789270803,1613806934,1194496402,1714732373,1320135280,1443511045,1216280897,1798539915,1710044952,1220130854,1244058664,2094083617,1337233917,1159402801,1719397245,1691761292,1804684191,1897526767,1216935175,1653530313,1660896292,1234172214,1915646905,1806519171,1712570933,1094212551,1253020831,1231821870,1191050205,1450409871,1311751174,1247811917,1733498464,1496855785,1702669051,2018274721,1579701382,1733299783,1732518843,2125419833,2191900615,2217010715,1301710240,1428240977,1679551850,2097247362,1319475951,1747095997,1780021887,1901896912,1233353122,1640016932,1486216264,1644579224,1316122930,1195190284,1747684497,1917680881,1266313744,1227500724,1510125855,1567686267,1229641152,1883617103,1689224772,1212569750,1658774702,1560151677,1727012530,1752820413,1216598840,1663665981,1216120480,2053061043,1198074851,1892269704,2191815965,1776903733,1214548420,1719830473,1713671073,1733015602,1604322600,2054942695,2044493631,1747215667,1711599694,1373539132,1942743372,1730113683,2258929822,1301405167,1198605313,1623895035,1251863630,1769333595,2118781902,1497034670,1644860771,1296175111,1742194724,1764855964,1655152542,1193250954,1750067330,1730342417,1729370543,1788168684,1099282035,1226826712,1884529763,1239368103,1692793532,1752001132,1800806323,1050746863,1912955724,1773581567,1998518201,1494856974," +
                       "2014595704,1275364395,1198503091,1752577890,1217933782,1888230280,1251670284,1820709070,1787399043,2326437920,1729342980,1752578111,1582330690,1224783537,1789342195,1447839285,1663355705,1767548212,1283512551,1941104461,1704658972,1905544097,2243297395,1449826590,1908197240,1736158193,1786213845,1660021331,1281798337,1246388563,1295560007,1461104390,1806498590,2092732297,1411845764,1659490917,1670700462,1213053171,1874319672,1344074947,1643200985,1770924910,1583038204,1222932097,1733086680,1972937844,1096536995,1394760615,1766228214,1685230080,1263303955,1925042183,1716761743,1783286485,1760212393,1734818644,1259110474,1350831670,1745464970,1258859614,1266333761,1704716073,2164787443,1215999281,1133549870,1895567845,1912768355,1398905345,2600208451,1258954655,1420657134,1732250173,1461308093,1253531973,1672785037,1734811050,1897745173,2024803027,1820779462,1805570632,1338738570,1579252671,1222605460,2134747882,1750071275,1923855543,1656214784,1758772173,1260256133,1349044517,1881270367,1728953197,1237024972,1248613470,1893754712,1796697540,1252411335,1895817327,1298574712,1867042172,1726663917,1066317210,1931321363,1333463273,1211397734,1592283013,1722207861,2191882801,1729043762,2035460720,1916655407,1682939805,1655537257,1711784061,1772361052,1967388380,1195813710,1649205627,2162809413,1708643783,2456021057,1872293074,1226604002,1280137272,1396728782,1249031993,1752510323,1578230251,1764963064,1884321274,1911296347,2357341220,1711194034,1571904063,1964113465,1731652994,1661763030,2026474373,1632849573,2085358211,1715308761,2159249065,1235576307,1566301073,1606301671,1299286183,1216171981,1867565545,2176204035,1653935934,1607701707,1894805985,1038224152,1719628403,1257831543,1719533787,1677319101,1781199803,1426725707,1880246651,1335005821,1823602784,1638761775,2445957923,1249629725,1802626467,1704959794,1135333261,1732032912,1252172351,1368260731,1941652581,1813361430,1907827305,1245910847,1809358527,1904330435,2474037313,1709735475,1653173290,1751657132,2249376727,1752051394,1817627763,2571515523,1226244970,1256917082,1799338427,1629966190,1300871220," +
                       "1750909545,1489698887,1252743172,2214969202,1604340290,1843638597,1240196953,1740019791,1320355271,1309588297,1210558765,1651123083,1363695267,1580853764,1676613997,1248483305,2360092592,1195910797,1648704543,1780676151,1898167171,1878113505,1923634871," +
                       "1832960512,1223732014,1720316187,1762161842,1765519035,1401921941,2374854813,1998525455,1510608922,1264425925,1263406744,1770713970,1640769615,1340529235,1763251165,1885148683,1568269950,1770829141,1660837984,1275931614,1413304027,1507326101,1210640567,1211836225,1747300565,2043643517,1733227047,1667632754,1764630572,1683796441,1744395855,2140261103,1686923643,1975548657,1137908300,1664279327,1731480580,1571801883,1265989743,1197051482,1655476125,1725416241,1234872834,1827931531,1769469074,1505167247,2115313227,1642749707,1930186570,1814841260,1194892683,1676664091,1670301037,1497144505,2419062270,1798550802,1400776392,1739046283,1523125043,1667664011,1336706552,1706491107,1705911330,1734575673,1702672140,1912140091,1401394915,1212569831,1192428237,1260156894,1277316722,1304204200,1080325345,1193692460,1249159055,1894711905,1273465467,1089476943,1762283363,1744465367,1936952034,1646704922,1294392841,1706813603,1034935880,1766053417,1880245795,2203025493,1217680514,1539060994,1691327452,1729950115,1648940111,1195228387,1173544654,1199712261,1444865141,1746301153,1274002264,1656705755,1746089664,1260158893,1732008061,1685508093,1872000325,1731313694,1281121664,1716235922,1741271603,1578214414,1400359365,1297798504,1841073657,1498760730,1617470687,1276097994,1774336991,1222062284,1288036110,1698299691,1912710131,1787051113,1272026444,1824963667,2292120434,1918992810,1243528091,1253686121,1337970873,1360746095,1236811171,1834076121,1761749934,1052767975,1880713160,1645804857,1905122791,1820599145,1660598463,1611662932,1363502007,1407297972,1700757973,1071853735,1933324977,1658169375,1797042937,1262687774,2191837071,1228183611,1192343191,1233628161,1865874465,1631018122,1238688811,2372849541,1713017142,1791282375,1747882057,2176246510,1255907130,2576582941,1191967613,1238726334,1407362151,1046222077,1750638262,1308929235,1712782607,1805588435,1667585650,1646493427,1224449043,1656182477,1650660520,1228945514,1363806760,1290442125,1285785310,1340552632,1689273620,2133607385,1810822404,1216826653,1400788390,2002265297,2130805237,1708317643,1345943410,1270750184," +
                       "1764535072,1631027997,1848693530,1074696355,1709002703,1252627151,1182357140,1791956201,1497351830,1860858420,1653578170,1729900362,1612183685,1656421625,2554404125,2021699397,1693909073,1397539371,1435160552,1680988091,1248793710,1293924474,1296760080,1231920714,1181410780,1697364500,1211241081,2502476613,1246147933,1607086767,1863792930,1211291140,1974602033,1591038901,1848304395,1740486065,1705258735,1405177363,2346370203,1739562541,1821525001,1227094453,2452982183,1837490440,1231654104,1245653042,1230280714,1713696033,1197961764,1437766763,1718101414,1756504117,1232040020,1196013440,1916659483,1403630461,1698250003,1378148655,1890308722,1528801650,1925127817,1917625235,1227092651,2111624400,1192502937,1078007814,1795177940,1497385885,1283723905,1790102710,1277319030,1752472834,1734589737,2044462285,1649971924,1629681802,1557858804,1558205014,1223540840,1259477352,1253587884,1346768403,1586018642,1865910037,1268017152,2464292797,1498615964,1650689355,1685719314,1264782967,1282868275,1213052292,1652731397,1678349244,1692815817,1190407882,1238873395,2034356794,1559608147,1901361077,1367557270,1744993875,1233614375,1751606144,1377828950,1066706794,1690293523,1296947890,1193486764,1212643341,1649354545,1750520665,1292380541,1698796205,1650633564,1689379471,1269162054,1886210881,1339702843,1600090264,2054996223,1707951780,2409790853,1305651895,1225563944,1744583555,1691043795,1832786147,1235733435,1633793811,1650718102,2041215745,1893658903,1865847353,1739957844,1191211465,1595131772,1913829237,1216463473,1767819164,1707732293,1641400562,1839047245,2008718405,1225930842,1876013145,2043915917,1770244095,2044426425,1567680564,1622749134,1703628822,1735625410,1738713232,1264775587,1842810834,1694788217,1334135721,1724187070,1268490765,1738006963,1810780601,1222601781,1410510003,1736068651,1247317642,1295718153,1733400841,1404794355,1554710050,1670905097,1266832557,1269086914,1809351112,1730443811,1227407741,1986578313,1737877151,2044473285,1751675285";

                    string[] splittopUid = topUid.Split(',');

                    for (int i = 0; i < splittopUid.Length; i++)
                    {
                        GetAtSomebodyFromUid(splittopUid[i]);
                    }

                });
            }));
            thLoad.IsBackground = true;
            thLoad.Start();  
        }
        #endregion

        #region Auth
        static bool GetAccessToken()
        {
            if (string.IsNullOrEmpty(AccessToken))
            {
                //iLogger.logger("配置文件中没有AccessToken！");
                //iLogger.logger("开始授权！");
                oauth = Authorize();
                oauth.ClientLogin(LoginName, PassWord);
                if (!string.IsNullOrEmpty(oauth.AccessToken))
                {
                    //iLogger.logger("获取AccessToken成功！ " + oauth.AccessToken);
                    ixml.Write("AccessToken", oauth.AccessToken);
                    //iLogger.logger("AccessToken已保存。");
                    return true;
                }
                else
                {
                    iLogger.logger("获取AccessToken失败！");
                    return false;
                }
            }
            else//如果配置文件中保存了AccesssToken
            {
                //iLogger.logger("配置文件中有AccessToken: " + AccessToken);
                oauth = new OAuth(AppKey, AppSecret, AccessToken, "");	//用Token实例化OAuth无需再次进入验证流程
                //iLogger.logger("验证Token有效性...");
                TokenResult result = oauth.VerifierAccessToken();	//测试保存的AccessToken的有效性
                if (result == TokenResult.Success)
                {
                    //iLogger.logger("AccessToken有效！");
                    return true;
                }
                else
                {
                    iLogger.logger("AccessToken无效！因为：" + result);
                    ixml.Write("AccessToken", string.Empty);
                    iLogger.logger("开始重新授权！");
                    oauth = Authorize();
                    if (!string.IsNullOrEmpty(oauth.AccessToken))
                    {
                        iLogger.logger("获取AccessToken成功！ " + oauth.AccessToken);
                        ixml.Write("AccessToken", oauth.AccessToken);
                        //iLogger.logger("AccessToken已保存。");
                        return true;
                    }
                    else
                    {
                        iLogger.logger("获取AccessToken失败！");
                        return false;
                    }
                }
            }

        }
        static OAuth Authorize()
        {
            OAuth o = new OAuth(AppKey, AppSecret, CallbackUrl);
            if (!o.ClientLogin(LoginName, PassWord))
            {
                iLogger.logger("授权登录失败，请检查配置是否正确！");
            }
            return o;
        }
        #endregion

        #region Timer
        static void TimerElapsedEvent(object sender, ElapsedEventArgs e)
        {
            //自定义时间任务
            DateTime iNow = DateTime.Now;
            string iYear = iNow.Year.ToString();
            string iMonth = iNow.Month.ToString();
            string iDay = iNow.Day.ToString();
            string iHour = iNow.ToString("HH");
            string iMinute = iNow.ToString("mm");
            string iSecond = iNow.Second.ToString();
            string sendTime = iHour + iMinute;

            List<string> listTimer = new List<string>();

            try
            {
                if (iNow.Month <= 1)
                {
                    listTimer.Add("0025");
                    listTimer.Add("0055");
                    listTimer.Add("0155");
                    listTimer.Add("0255");
                    listTimer.Add("0355");
                    listTimer.Add("0455");
                    listTimer.Add("0555");
                    listTimer.Add("0655");
                    listTimer.Add("0725");
                    listTimer.Add("0755");
                    listTimer.Add("0825");
                    listTimer.Add("0855");
                    listTimer.Add("0925");
                    listTimer.Add("0955");
                    listTimer.Add("1025");
                    listTimer.Add("1055");
                    listTimer.Add("1125");
                    listTimer.Add("1155");
                    listTimer.Add("1215");
                    listTimer.Add("1235");
                    listTimer.Add("1255");
                    listTimer.Add("1315");
                    listTimer.Add("1335");
                    listTimer.Add("1355");
                    listTimer.Add("1425");
                    listTimer.Add("1455");
                    listTimer.Add("1525");
                    listTimer.Add("1555");
                    listTimer.Add("1625");
                    listTimer.Add("1655");
                    listTimer.Add("1715");
                    listTimer.Add("1735");
                    listTimer.Add("1755");
                    listTimer.Add("1815");
                    listTimer.Add("1835");
                    listTimer.Add("1855");
                    listTimer.Add("1915");
                    listTimer.Add("1935");
                    listTimer.Add("1955");
                    listTimer.Add("2015");
                    listTimer.Add("2035");
                    listTimer.Add("2055");
                    listTimer.Add("2115");
                    listTimer.Add("2135");
                    listTimer.Add("2155");
                    listTimer.Add("2215");
                    listTimer.Add("2235");
                    listTimer.Add("2255");
                    listTimer.Add("2315");
                    listTimer.Add("2335");
                    listTimer.Add("2355");
                    listTimer.Add("2355");

                    foreach (string curTime in listTimer)
                    {
                        if (curTime == sendTime) // Will match once
                        {
                            if (iNow.Hour > 1 && iNow.Hour < 5)
                            {
                                SyncWeibo("2313896275"); //未满十八周岁者禁止关注
                            }
                            else
                            {
                                SyncWeibo("1713926427"); //微博搞笑排行榜 
                            }
                            break;
                        }
                    }
                }
                else
                {
                    Dictionary<string, string> twitter_tbs = iSqlite.SearchCurrentWeibo(dataBaseFile);
                    if (twitter_tbs.Count > 0)
                    {
                        if (twitter_tbs["Forward"] == "")
                        {
                            iLogger.logger("获得发布内容。类型：" + ((twitter_tbs["Picture"] == "") ? "Text" : "Text + Picture"));
                        }
                        else
                        {
                            iLogger.logger("获得转发内容。");
                        }
                        SendWeibo(twitter_tbs);
                    }
                }

            }
            catch (Exception ex)
            {
                iLogger.logger("定时任务异常：" + ex.Message);
            }
        }
        #endregion

        #region AT
        static void GetAtSomebodyFromUid(string topUid)
        {
            if (atCount >= 100000)
            {
                return;
            }
            try
            {
                IEnumerable<NetDimension.Weibo.Entities.user.Entity> activeIds = Sina.API.Entity.Friendships.FollowersInActive(topUid, 200);
                foreach (var activeId in activeIds)
                {
                    DateTime dtNow = DateTime.Now;
                    //DateTime dtCreate = DateTime.ParseExact("Fri Dec 21 09:30:23 +0800 2012", "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
                    DateTime dtCreate = DateTime.ParseExact(activeId.Status.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
                    TimeSpan ts1 = new TimeSpan(dtNow.Ticks);
                    TimeSpan ts2 = new TimeSpan(dtCreate.Ticks);
                    TimeSpan ts = ts1.Subtract(ts2).Duration();

                    if (Convert.ToInt32(activeId.FollowersCount) > 1000 //粉丝数大于3000
                        && (ts.Days < 3)                                //最近更新小于3天
                        && (activeId.ScreenName.Length < 10)            //名字长度小于10
                        && !Regex.IsMatch(activeId.ScreenName, "用户|粉|最|城|市|省|国|警|商|司|车|官|微|榜|娱|笑|店|团|牌|超|潮|典|购|折|集|禁|精|录|尚|搜|食|肥|瘦|淘|藤|网|裙|衣|鞋|秒|新", RegexOptions.IgnoreCase)
                        )
                    {
                        atCount += 1;
                        bool getduplicated = false;
                        foreach (string curUid in listx) // Loop through List with foreach
                        {
                            if (curUid == activeId.ID)
                            {
                                getduplicated = true;
                                break;
                            }
                        }
                        if (!getduplicated)
                        {
                            listx.Add(activeId.ID);
                            iLogger.logger(atCount.ToString() + "\tu:" + activeId.ID + " f:" + activeId.FollowersCount + "\tn:" + activeId.ScreenName);
                            if (activeId.FollowersCount > 3000)
                            {
                                Thread.Sleep(ro.Next(15000, 30000));
                                GetAtSomebodyFromUid(activeId.ID);
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                string abcd = ex.Message;
            }
        }
        static string GetATSomboby()
        {
            try
            {
                StreamReader objReader = new StreamReader("AtList.txt");
                string sLine = "";
                ArrayList LineList = new ArrayList();
                while (sLine != null)
                {
                    sLine = objReader.ReadLine();
                    if (sLine != null && !sLine.Equals(""))
                        LineList.Add(sLine);
                }
                objReader.Close();
                return LineList[ro.Next(0, LineList.Count - 1)].ToString();
            }
            catch (Exception ex)
            {
                iLogger.logger("GetATSomboby error " + ex.Message);
                return string.Empty;
                //throw(new Exception("GetATSomboby error " + ex.Message);
            }

        }
        static string AddATSomebody(string weiboText)
        {
            string result = string.Empty;
            try
            {
                string sb = GetATSomboby();

                if (weiboText.Length > 90 || weiboText.Contains("@"))
                {
                    return weiboText;
                }
                else if (Regex.IsMatch(weiboText, "（转）|「转」|\\(转\\)"))
                {

                    result = Regex.Replace(weiboText, "（转）|「转」|\\(转\\)", (sb != "") ? (" @" + sb + " ") : "", RegexOptions.IgnoreCase);
                    return result;
                }
                else if (Regex.IsMatch(weiboText, "via", RegexOptions.IgnoreCase))
                {
                    result = Regex.Replace(weiboText, "via |via", "via @", RegexOptions.IgnoreCase);
                    return result;
                }
                else if (Regex.IsMatch(weiboText, "by", RegexOptions.IgnoreCase))
                {
                    result = Regex.Replace(weiboText, "by |by", "by @", RegexOptions.IgnoreCase);
                    return result;
                }
                else
                {
                    result = weiboText + " @" + sb;
                }

            }
            catch (Exception ex)
            {
                iLogger.logger("AddATSomebody error " + ex.Message);
                result = weiboText;
                //throw (new Exception("AddATSomebody: " + text + ", Error:" + ex.Message));
            }
            return result;
        }
        static string RemoveATSomebody(string text)
        {
            string result = string.Empty;
            try
            {
                result = Regex.Replace(text, "@.+?投稿", "", RegexOptions.IgnoreCase);
            }
            catch (Exception ex)
            {
                throw (new Exception("Regex.Replace: " + text + ", Error:" + ex.Message));
            }
            return result;
        }
        #endregion AT

        #region Weibo
        static void SyncWeibo(string uid)
        {
            DateTime dtNow = DateTime.Now;
            bool forward = false;
            int checkDuplicateLength = 5;
            //DateTime dtCreate = DateTime.ParseExact("Fri Dec 21 09:30:23 +0800 2012", "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);

            NetDimension.Weibo.Entities.status.Collection myTimeLine = Sina.API.Entity.Statuses.UserTimeline(myUid, "", "", "", 50);
            NetDimension.Weibo.Entities.status.Collection hisTimeLine = Sina.API.Entity.Statuses.UserTimeline(uid, "", "", "", 50);

            //如果自已发的微博比较热门，就自己转发一次
            foreach (var myItem in myTimeLine.Statuses)
            {
                DateTime dtCreate = DateTime.ParseExact(myItem.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
                TimeSpan ts1 = new TimeSpan(dtNow.Ticks);
                TimeSpan ts2 = new TimeSpan(dtCreate.Ticks);
                TimeSpan ts = ts1.Subtract(ts2).Duration();

                if (ts.TotalHours > 23.9 && ts.TotalHours < 24.1 && myItem.RepostsCount >= 3)
                {
                    forward = true;
                    Dictionary<string, string> twitter_tbs = new Dictionary<string, string>();
                    twitter_tbs.Add("Forward", iMidToId.Id2Mid(Convert.ToInt64(myItem.ID)));
                    twitter_tbs.Add("Text", "");
                    twitter_tbs.Add("Picture", "");
                    SendWeibo(twitter_tbs);
                    break;
                }
            }

            //没有获得转发
            if (!forward)
            {
                //检查是否重复
                foreach (var hisItem in hisTimeLine.Statuses)
                {
                    bool getDuplicated = false;

                    DateTime dtCreate = DateTime.ParseExact(hisItem.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
                    TimeSpan ts1 = new TimeSpan(dtNow.Ticks);
                    TimeSpan ts2 = new TimeSpan(dtCreate.Ticks);
                    TimeSpan ts = ts1.Subtract(ts2).Duration();

                    if (hisItem.RetweetedStatus != null || ts.TotalHours > 24 || hisItem.RepostsCount < 1000
                        || Regex.IsMatch(hisItem.Text, "笑话|淘宝|网购|下载|推荐|私信|更多|夜猫子们|赐句|果粉|推荐|错误|安卓|app|ＡＰＰ|免邮|面膜|营销|代言|参加|免费|星座|\\#|卫视|物流|客服|联通|《|》|汽车"))
                    {
                        continue;
                    }
                    foreach (var myItem in myTimeLine.Statuses)
                    {
                        if (hisItem.Text.Contains(myItem.Text.Substring(0, (myItem.Text.Length < checkDuplicateLength) ? myItem.Text.Length : checkDuplicateLength)))
                        {
                            getDuplicated = true;
                            break;
                        }
                    }
                    if (!getDuplicated)
                    {
                        Dictionary<string, string> twitter_tbs = new Dictionary<string, string>();
                        twitter_tbs.Add("Forward", string.Empty);
                        twitter_tbs.Add("Text", AddATSomebody(hisItem.Text));
                        twitter_tbs.Add("Picture", hisItem.OriginalPictureUrl);
                        SendWeibo(twitter_tbs);
                        break;
                    }
                }
            }

        }
        static void SendWeibo(Dictionary<string, string> twitter)
        {
            NetDimension.Weibo.Entities.status.Entity statusInfo = null;
            try
            {
                //转发
                if (!String.IsNullOrEmpty(twitter["Forward"]))
                {
                    statusInfo = Sina.API.Entity.Statuses.Repost(iMidToId.Mid2Id(twitter["Forward"]), twitter["Text"], 3);
                }
                //发送
                else
                {
                    if (!String.IsNullOrEmpty(twitter["Picture"]))
                    {
                        //高级授权，直接调用高级接口发送 图文微博,接口有问题，发送很高的图片，会被缩小
                        if (false && Regex.IsMatch(AppKey, "2190063733|3510766076|783190658|2440435914|1262673699|1965726745|2027761570|2323547071", RegexOptions.IgnoreCase))
                        {
                            statusInfo = Sina.API.Entity.Statuses.UploadUrlText(twitter["Text"], twitter["Picture"]);
                        }
                        else //否则自己下载图片，本地上传图片发送
                        {
                            string DownloadedFile = iPicture.DownloadPicture(twitter["Picture"]);
                            if (AutoWaterMark && DownloadedFile.Contains(".jpg"))
                            {
                                string FileWithWater = iWaterMark.AddImageSignPic(DownloadedFile, waterMarkFile, 9, 100, 6);
                                DownloadedFile = FileWithWater;
                            }
                            if (DownloadedFile != "")
                            {
                                byte[] OpenedPicture = iPicture.OpenFile(DownloadedFile);
                                if (OpenedPicture != null)
                                {
                                    statusInfo = Sina.API.Entity.Statuses.Upload(twitter["Text"], OpenedPicture);
                                }
                            }
                            else
                            {
                                iLogger.logger("错误，DownloadedFile:" + DownloadedFile);
                            }
                        }
                    }
                    else
                    {
                        statusInfo = Sina.API.Entity.Statuses.Update(twitter["Text"]);
                    }
                }
                
                if (statusInfo != null)
                {
                    iLogger.logger("发布成功。MID:" + iMidToId.Id2Mid(Convert.ToInt64(statusInfo.ID)) + ", Text:" + statusInfo.Text.Substring(0, (statusInfo.Text.Length < 10)? statusInfo.Text.Length: 10));
                    //自动评论李开复
                    if (AutoComment)
                    {
                        CommnetWeiboViaUid("1197161814", statusInfo.Text.Substring(0, (statusInfo.Text.Length < 100) ? statusInfo.Text.Length : 100) + " 更多笑话请关注我的微博..."); 
                    }

                    
                }
            }
            catch (WeiboException ex)
            {
                iLogger.logger("发布失败！错误码：" + ex.ErrorCode + " 原因: " + ex.Message);
                //如果授权失败则重新获取授权！
                if (ex.ErrorCode == "21319")
                {
                    iLogger.logger("开始重新授权");
                    oauth = Authorize();
                    if (!string.IsNullOrEmpty(oauth.AccessToken))
                    {
                        iLogger.logger("重新获取AccessToken成功！ " + oauth.AccessToken);
                        ixml.Write("AccessToken", oauth.AccessToken);
                        iLogger.logger("AccessToken已保存。");
                    }
                    else
                    {
                        iLogger.logger("重新获取AccessToken失败！");
                    }
                }
            }
            catch (Exception ex)
            {
                iLogger.logger("其它错误：" + ex.Message);
            }
        }
        static void CommnetWeiboViaUid(string uid, string comment)
        {
            try
            {
                NetDimension.Weibo.Entities.user.Entity usershow = Sina.API.Entity.Users.Show(uid);
                NetDimension.Weibo.Entities.comment.Entity createComment = Sina.API.Entity.Comments.Create(usershow.Status.ID, comment);
                iLogger.logger("评论成功。MID:" + iMidToId.Id2Mid(Convert.ToInt64(createComment.ID)) + ", Text:" + createComment.Text.Substring(0, (createComment.Text.Length < 10) ? createComment.Text.Length : 10));
            }
            catch (WeiboException ex)
            {
                iLogger.logger("评论失败！错误码：" + ex.ErrorCode + " 原因: " + ex.Message);
            }
            catch (Exception ex)
            {
                iLogger.logger("其它错误：" + ex.Message);
            }
        }
        static void CommnetWeiboViaMid(string mid, string comment)
        {
            try
            {
                NetDimension.Weibo.Entities.comment.Entity createComment = Sina.API.Entity.Comments.Create(mid, comment);
                iLogger.logger("评论成功。MID:" + iMidToId.Id2Mid(Convert.ToInt64(createComment.ID)) + ", Text:" + createComment.Text.Substring(0, (createComment.Text.Length < 10) ? createComment.Text.Length : 10));
            }
            catch (WeiboException ex)
            {
                iLogger.logger("评论失败！错误码：" + ex.ErrorCode + " 原因: " + ex.Message);
            }
            catch (Exception ex)
            {
                iLogger.logger("其它错误：" + ex.Message);
            }
        }
        #endregion
      
        #region COMMONTS 评论
        static void LeaveMessage2PotentialCustomer()
        {
            int commentTimes = 0;
            bool method1 = true;
            //uint[] EnglishDaHao = { 2707563273, 1699451730, 1649977267, 1900887504, 2032414481, 2196649937, 1708242827, 1939417770, 2608436611, 2452849660, 2415967783, 2008644587, 2610990327, 2336769711, 1990139112, 1804522700, 1904271007, 2508522380, 2708561137, 1908763543, 2415965727, 1997631353, 1763254735, 2613101797, 2158792102, 2088963285, 2032663345, 2012028544, 2166360410, 2050018463, 1790424043, 2498747702, 2076719205, 1958372753, 2502349331, 1897256622, 2163511854, 2324920652, 2477938850, 2156978280, 2355755720, 2527825752 };
            uint[] EnglishDaHao = { 2707563273, 1699451730, 1649977267, 1900887504, 2032414481, 2196649937, 1708242827, 1939417770, 2608436611, 2452849660, 2415967783, 2008644587, 2610990327, 2336769711, 1990139112, 1804522700, 1904271007, 2508522380, 2708561137, 1908763543, 2415965727, 1997631353, 1763254735, 2613101797, 2158792102, 2088963285, 2032663345, 2012028544, 2166360410, 2050018463, 1790424043, 2498747702, 2076719205, 1958372753, 2502349331, 1897256622, 2163511854, 2324920652, 2477938850, 2156978280, 2355755720, 2527825752 };
            for (int idIndex = 0; idIndex < EnglishDaHao.Length; idIndex++)
            {
                try
                {
                    if (method1)
                    {
                        //method 1: 获取最近的5000个粉丝id号码
                        NetDimension.Weibo.Entities.user.IDCollection ids = Sina.API.Entity.Friendships.FollowerIDs(EnglishDaHao[idIndex].ToString(), "", 5000);
                        foreach (var id in ids.Users)
                        {
                            iLogger.logger("ID:" + id.ToString());
                        }
                        foreach (var id in ids.Users)
                        {
                            NetDimension.Weibo.Entities.user.Entity usershow = Sina.API.Entity.Users.Show(id);
                            NetDimension.Weibo.Entities.comment.Entity createComment = Sina.API.Entity.Comments.Create(usershow.Status.ID, "@" + usershow.ScreenName + " 欢迎关注我的英语微博，及微信: YoudaoEnglish，二维码链接：http://weibo.com/2480531040/z9f5UfaTx");
                            commentTimes++;
                            iLogger.logger("评论发送成功。ID:" + createComment.ID + ", Text:" + createComment.Text.Substring(0, 10));
                            Thread.Sleep(ro.Next(3000, 5000));
                        }
                    }
                    else
                    {
                        //method 2: //获取最活跃的200个粉丝账号的信息，包括id和name
                        string atSomebody = "";
                        IEnumerable<NetDimension.Weibo.Entities.user.Entity> activeIds = Sina.API.Entity.Friendships.FollowersInActive(EnglishDaHao[idIndex].ToString(), 200);
                        foreach (var activeId in activeIds)
                        {
                            if (atSomebody.Length < 103)
                            {
                                atSomebody += "@" + activeId.ScreenName;
                            }
                            else
                            {
                                Thread.Sleep(ro.Next(1000, 5000));
                                iLogger.logger("提醒列表：" + atSomebody);
                                //NetDimension.Weibo.Entities.comment.Entity createComment = Sina.API.Entity.Comments.Create(Mid2Id("z9f5UfaTx").ToString(), atSomebody);
                                //logger("评论发送成功。ID:" + createComment.ID + ", Text:" + createComment.Text.Substring(0, 10));
                                atSomebody = "";
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    iLogger.logger("评论发送异常：" + ex.Message);
                }
            }


        }
        #endregion

        #region FollowFans 刷粉
        static void FollowFans()
        {
            string friends;

            friends = "3073482613,3072170094,3058841503,3049216512,3017009123,2972523311,2966195094,2960800774,2923512455,2917906965,2832958644,2818152600,2815167601,2809212554,2801452484,2798357760,2787395614,2783285393,2774904113,2774745304,2752271731,2749563644,2737339251,2733765974,2714343021,2710843953,2634433450,2616703613,2549068941,2501254184,2487292540,2450147742,2433189657,2413551121,2409453193,2394950217,2337690312,2334118401,2332870591,2250931782,2242474643,2219688753,2208825400,2180637767,2176602000,2173529342,2157801852,2157318342,2157159724,2143132691,2131338890,2107421700,2104227595,2101271997,2090640340,2090348881,2012261694,1980108320,1972366421,1969187745,1936013750,1930052821,1926060444,1908265350,1893624083,1862317743,1852154047,1820215017,1812156470,1799864220,1776387945,1768319692,1738691715,1696121821,1689586854,1675695981,1649347077,1646754611,1644689407,1568655142,1557410553,1463615545,1396560085,1345727452";
            string[] splitFriend = friends.Split(',');

            int waitTime = 5000;
            int followCount = 0;

            for (int i = 0; i < splitFriend.Length; i++)
            {
                if (followCount <= 158)
                {
                    try
                    {
                        Thread.Sleep(ro.Next(15000, 30000));
                        var relation = Sina.API.Entity.Friendships.Show(myUid, "", splitFriend[i], "");
                        if (!relation.Source.Following && !Regex.IsMatch(relation.Target.ScreenName, "购物|返利|兼职|招聘|营销|团购|精选", RegexOptions.IgnoreCase))
                        {
                            var entity = Sina.API.Entity.Friendships.Create(splitFriend[i], "");
                            iLogger.logger("关注了：" + i.ToString() + " / " + splitFriend.Length.ToString() + " " + splitFriend[i] + "|" + entity.Name);
                            followCount++;
                        }
                        else
                        {
                            iLogger.logger("不关注：" + i.ToString() + " / " + splitFriend.Length.ToString() + " " + splitFriend[i] + "|" + relation.Target.ScreenName);
                        }

                    }
                    catch (WeiboException ex)
                    {
                        iLogger.logger("weibo错误：" + ex.Message);
                        if (ex.ErrorCode == "21332")
                        {
                            GetAccessToken();
                            Sina = new Client(oauth);
                            string uid2 = Sina.API.Entity.Account.GetUID();
                            var entity_userInfo = Sina.API.Entity.Users.Show(myUid);
                            iLogger.logger("uid：" + uid2 + ", 昵称：" + entity_userInfo.ScreenName);
                        }
                        iLogger.logger("等待：" + waitTime.ToString());
                        Thread.Sleep(waitTime);
                        waitTime += 5000;
                    }
                    catch (Exception ex)
                    {
                        iLogger.logger("other 错误：" + ex.Message);
                    }
                }
                else
                {
                    iLogger.logger("当前关注" + followCount.ToString());
                    break;
                }
            }
            Console.ReadKey(); //防止程序自行退出
        }
        #endregion

        #region Tray & Menu 托盘与菜单
        //还原窗体
        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            //判断是否已经最小化于托盘 
            if (WindowState == FormWindowState.Minimized)
            {
                //还原窗体显示 
                WindowState = FormWindowState.Normal;
                //激活窗体并给予它焦点 
                this.Activate();
                //任务栏区显示图标 
                this.ShowInTaskbar = true;
                //托盘区图标隐藏 
                notifyicon.Visible = false;
            }
        }

        //隐藏任务栏图标、显示托盘图标
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            //判断是否选择的是最小化按钮 
            if (WindowState == FormWindowState.Minimized)
            {
                //托盘显示图标等于托盘图标对象 
                //注意notifyIcon1是控件的名字而不是对象的名字 
                //notifyIcon1.Icon = ico;
                //隐藏任务栏区图标 
                this.ShowInTaskbar = false;
                //图标显示在托盘区 
                notifyicon.Visible = true;
            }
        }

        //打开日志
        private void openLoggerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DateTime currentTime = DateTime.Now;
            string str = Directory.GetCurrentDirectory();
            string fname = currentTime.ToString("yyyy") + currentTime.ToString("MM") + ".log";
            string logfile = str + "\\" + fname;
            Process.Start("notepad.exe", logfile);
        }
        //转到微博
        private void goToWeiboToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string myWeibo = "http://weibo.com/" + myUid;
            Process.Start(myWeibo);
        }
        //退出程序
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.GetCurrentProcess().Kill();
        }
        #endregion
    }
}
